# Contributor: Nirosan <pnirosan@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=R
pkgver=4.4.1
pkgrel=0
pkgdesc="Language and environment for statistical computing"
url="https://www.r-project.org/"
# riscv64: blocked by java-jre-headless
# loongarch64: test failure
arch="all !riscv64"
license="( GPL-2.0-only OR GPL-3.0-only ) AND LGPL-2.1-or-later"
depends="$pkgname-mathlib"
depends_dev="
	bzip2-dev
	curl-dev>=7.28
	gcc
	gfortran
	icu-dev
	libdeflate-dev
	libjpeg-turbo
	libpng-dev
	make
	musl-dev
	openblas-dev>=0.3.0
	pcre2-dev
	readline-dev
	xz-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	cairo-dev
	java-jdk
	libxmu-dev
	pango-dev
	perl
	tiff-dev
	tk-dev
	"
install="$pkgname.post-install"
subpackages="$pkgname-mathlib $pkgname-dev:_dev $pkgname-doc"
source="https://cran.r-project.org/src/base/R-${pkgver%%.*}/R-$pkgver.tar.gz"

_rhome="usr/lib/R"
ldpath="/$_rhome/lib"

build() {
	# Performance is more important than size for R. Moreover, -O2 has
	# only minimal impact on the R package size (less than 1 %).
	export CFLAGS="${CFLAGS/-Os/-O2}"
	export CPPFLAGS="${CPPFLAGS/-Os/-O2}"
	export CXXFLAGS="${CXXFLAGS/-Os/-O2}"

	# CXXFLAGS is propagated to /etc/R/Makeconf that is read when building
	# additional R modules. -D__MUSL__ is needed for some modules like Rcpp.
	# htps://github.com/RcppCore/Rcpp/issues/448
	export CXXFLAGS="$CXXFLAGS -D__MUSL__"

	r_cv_have_curl728=y \
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/R \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--libdir=/usr/lib \
		rdocdir=/usr/share/doc/R \
		rincludedir=/usr/include/R \
		rsharedir=/usr/share/R \
		--disable-nls \
		--enable-R-shlib \
		--enable-java \
		--enable-lto \
		--without-recommended-packages \
		--with-blas=openblas \
		--with-cairo \
		--with-ICU \
		--with-jpeglib \
		--with-lapack \
		--with-libpng \
		--with-libtiff \
		--with-tcltk \
		--with-x

	make
	make -C src/nmath/standalone
}

check() {
	# Try running R (and also aid debugging).
	./bin/R --vanilla -s -e 'sessionInfo(); R_compiled_by(); str(.Machine)'

	case "$CARCH" in
		# A (non-portable) numerical regression test currently (R 4.4.0)
		# fails on the aarch64, loongarch64 and s390x runners.
		# Run at least two basic test suites:
		aarch64 | s390x | loongarch64)
			make -C tests test-Examples && make -C tests test-Specific || {
				tail -v -n 42 tests/*.fail; exit 1
			}
		;;
		*)
			make check || {
				tail -v -n 42 tests/*.fail; exit 1
			}
		;;
	esac

	# NOTE: EST5EDT-related differences reported for datetime.Rout are known
	# (see <https://mm.icann.org/pipermail/tz/2024-March/058740.html>
	# or also <https://bugs.r-project.org/show_bug.cgi?id=16843#c12>)
}

package() {
	local destdir="$pkgdir/$_rhome"

	make DESTDIR="$pkgdir" install

	# Install libRmath.so.
	cd src/nmath/standalone
	make DESTDIR="$pkgdir" install
	cd -

	# Fixup R wrapper script (taken from Arch).
	rm "$destdir"/bin/R
	ln -sf /usr/bin/R "$destdir"/bin/R

	# Remove some useless files (COPYING is duplicated, it will be
	# in -doc, don't worry).
	rm "$destdir"/COPYING "$destdir"/SVN-REVISION

	mkdir -p "$pkgdir"/etc/R

	# R apparently ignores --sysconfdir, so we must manually move configs
	# to /etc/R and make symlinks.
	cd "$destdir"/etc
	local f; for f in *; do
		mv "$f" "$pkgdir"/etc/R/ && ln -sf /etc/R/$f $f
	done
	cd -
}

mathlib() {
	pkgdesc="Standalone math library from the R project"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libRmath.so* "$subpkgdir"/usr/lib
}

_dev() {
	depends="R=$pkgver-r$pkgrel"
	default_dev
}

sha512sums="
cc30172227b7d89a9d6e9d65c8622b2042bcbbf3f1b5e36c7460f60958a1a0589980a1867ec9989df2589c1618233f3382d4732700b147361ace3be6680b5643  R-4.4.1.tar.gz
"
